package com.aatattoo.light_food_service.filter;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
public class TokenConfiguration extends WebMvcConfigurerAdapter {
    @Bean
    TokenInterceptor tokenInterceptor() {
        return new TokenInterceptor();
        // 这个方法才能在拦截器中自动注入查询数据库的对象
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry ){
//        registry.addInterceptor(tokenInterceptor()).addPathPatterns("/user/*",
//                "/comment/*",
//                "/dynamic/*",
//                "/information/*",
//                "/upload/*",
//                "/sms/*",
//                "/function/*")
        registry.addInterceptor(tokenInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/user/avatar/*",
                        "/error",
                        "/dynamic/img/*",
                        "/food/img/*",
                        "/**/*.html",
                        "/**/*.js",              //js静态资源
                        "/**/*.css"             //css静态资源
                      );
    }

    //添加放行静态资源
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //文件磁盘图片url 映射
        //配置server虚拟路径，handler为前台访问的目录，locations为files相对应的本地路径
        registry.addResourceHandler("/user/avatar/*").addResourceLocations("file:/uploadfiles/userImg/");
//        registry.addResourceHandler("/user/avatar/*").addResourceLocations("file:/Users/aatattoo/Desktop/file/");
        registry.addResourceHandler("/dynamic/img/*").addResourceLocations("file:/uploadfiles/dynamicImg/");
        registry.addResourceHandler("/food/img/*").addResourceLocations("file:/uploadfiles/foodImg/");
        //配置静态文件路径，与上面并不冲突
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/static/index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
}

